<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图书管理</el-breadcrumb-item>
      <el-breadcrumb-item>图书分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getCategoryList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getCategoryList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
    </el-card>
    <!-- 用户列表(表格)区域 -->
    <el-table :data="categoryList" border stripe>
      <el-table-column type="index" label="索引"></el-table-column>
      <el-table-column label="分类名称" prop="category_name"></el-table-column>
      <el-table-column
        label="修改时间"
        prop="category_update"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="修改"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="getCategoryById(scope.row.category_id)"
            ></el-button>
          </el-tooltip>
          <!-- 删除 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteCategory(scope.row.category_id)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加用户对话框区域 -->
    <!-- 对话框组件  :visible.sync(设置是否显示对话框) width(设置对话框的宽度) :before-close(在对话框关闭前触发的事件) -->
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="addDialogClose"
    >
      <!-- 对话框主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="category_name">
          <el-input v-model="addForm.category_name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 对话框底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClose"
    >
      <!-- 对话框主题区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="分类Id">
          <el-input v-model="editForm.category_id" disabled></el-input>
        </el-form-item>
        <el-form-item label="分类名称" prop="category_name">
          <el-input v-model="editForm.category_name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 对话框底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: ''
      },
      categoryList: [],
      // 添加对话框
      addDialogVisible: false,
      addForm: {},
      addFormRules: {
        category_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      // 修改对话框
      editDialogVisible: false,
      editForm: {},
      editFormRules: {
        category_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    // 获取分类列表
    async getCategoryList() {
      const { data: res } = await this.$http.get('category/getCategoryByName', {
        params: this.queryInfo
      })
      this.categoryList = res.data
    },
    // 添加分类
    addCategory() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        this.addForm.category_update = this.getNowFormatDate()
        const { data: res } = await this.$http.post(
          '/category/addCategory',
          this.addForm
        )
        if (res.code !== 200) return this.$message.error('添加失败')
        this.$message.success('添加成功')
        this.addDialogVisible = false
        this.getCategoryList()
      })
    },
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    async getCategoryById(categoryId) {
      this.editDialogVisible = true
      const { data: res } = await this.$http.get(
        '/category/getCategoryById' + '/' + categoryId
      )
      this.editForm = res.data[0]
    },
    // 编辑分类
    editCategory(categoryId) {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        this.editForm.category_update = this.getNowFormatDate()
        const { data: res } = await this.$http.put(
          '/category/updateCategory',
          this.editForm
        )
        if (res.code !== 200) return this.$message.error('修改失败')
        this.$message.success('修改成功')
        this.editDialogVisible = false
        this.getCategoryList()
      })
    },
    editDialogClose() {
      this.$refs.editFormRef.resetFields()
    },
    // 删除分类
    async deleteCategory(categoryId) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('/category/deleteCategory', {
        category_id: categoryId
      })
      if (res.code !== 200) return this.$message.error('删除分类失败')
      this.$message.success('删除分类成功')
      this.getCategoryList()
    }
  }
}
</script>

<style></style>
